//
//  AddContactCell.swift
//  旅航ios
//
//  Created by tulip on 2021/5/7.
//

import UIKit
import SwiftyJSON

class AddPassagerCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
    var propPassagerIDArr:((_ arr:Array<Int>,_ dicArr:Array<Dictionary<String,Any>>)->Void)!
    var idArr:Array<Int>=[]
    var infoDicArr:Array<Dictionary<String,Any>>=[]
    var arr:Array<PassagerCell>=[]
    var data:[JSON]=[]
    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var addBtn: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        addBtn.layer.cornerRadius=6
        addBtn.backgroundColor=UIColor.init(hexString: "e3eeff")
        self.backgroundColor=UIColor.clear
        self.selectionStyle = .none
        
        table.register(UINib(nibName: "PassagerCell", bundle: nil), forCellReuseIdentifier: "PassagerCell")
        table.backgroundColor=UIColor.clear
        table.showsVerticalScrollIndicator=false
        table.delegate=self
        table.dataSource=self
        table.bounces=false
        
        Network.request(.getPassager(parameters: ["token" : TOKEN])) { (JSON) in
            self.data =  JSON["data"].array ?? []
            self.table.reloadData()
        } failure: { (err) in
            
        }
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBAction func add(_ sender: Any) {
        let vc=ChoosePassagerVC.init()
        getCurrentVc().navigationController?.pushViewController(vc, animated: true)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:PassagerCell=tableView.dequeueReusableCell(withIdentifier: "PassagerCell", for: indexPath) as! PassagerCell
        cell.name.text=data[indexPath.row]["name"].string
        let idstr=data[indexPath.row]["idcard"].string ?? ""
        
        let doument_type=data[indexPath.row]["doument_type"].int ?? 1
        cell.idTypeNum=doument_type
        cell.nameStr=data[indexPath.row]["name"].string ?? ""
        cell.idNum=idstr
        cell.phoneStr=data[indexPath.row]["mobile"].string ?? ""
        cell.id=data[indexPath.row]["id"].int ?? 0
      //证件类型 1身份证 2护照 3军人证 4回乡证 5台胞证 6外国人永久居留证 99 其他
        switch doument_type {
        case 1:
            cell.idCard.text="身份证 " + idstr
        case 2:
            cell.idCard.text="护照 " + idstr
        case 3:
            cell.idCard.text="军人证 " + idstr
        case 4:
            cell.idCard.text="回乡证 " + idstr
        case 5:
            cell.idCard.text="台胞证 " + idstr
        case 6:
            cell.idCard.text="外国人永久居留证 " + idstr
        case 99:
            cell.idCard.text="其他 " + idstr
        default:
            break
        }
        
        cell.edit.addTarget(self, action: #selector(editPassager), for: .touchUpInside)
        cell.edit.mdic=["idTypeNum":cell.idTypeNum,"idNum":cell.idNum,"name":cell.nameStr,"phone":cell.phoneStr]
        cell.mdic=["idTypeNum":cell.idTypeNum,"idNum":cell.idNum,"name":cell.nameStr,"phone":cell.phoneStr]
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell:PassagerCell=tableView.cellForRow(at: indexPath) as! PassagerCell
        if arr.contains(cell){
            cell.chooseBtn.isSelected=false
            
         arr = arr.filter({ return $0 !== cell })
         idArr=idArr.filter({ return $0 != cell.id})
            infoDicArr=infoDicArr.filter({return $0["idNum"]  as? String != cell.mdic["idNum"]  as? String})
            
            if propPassagerIDArr != nil{
                propPassagerIDArr(idArr,infoDicArr)
            }
            
        }else{
           
            cell.chooseBtn.isSelected=true
            arr.append(cell)
            idArr.append(cell.id)
            infoDicArr.append(cell.mdic)
            
            if propPassagerIDArr != nil{
                propPassagerIDArr(idArr,infoDicArr)
            }
        }
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        80
    }
 
    @objc func editPassager(sender:UIButton){
        let vc=EditPassagerVC.init()
        vc.isEdit=true
        vc.sendDic=sender.mdic
        
        getCurrentVc().navigationController?.pushViewController(vc, animated: true)
        
    }
    
}
